﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using WindowsPhoneRssReader.Models;

namespace WindowsPhoneRssReader
{
    public partial class MainPage : PhoneApplicationPage
    {

        // Constructor
        private object _selectedItem;
        private RssReader model;

        /*
         * choose uri to navigate
         *  1. FeedItemDetailsPage.xaml
         *  2. FeedDetailsPage.xaml
         */
        private short navigateToArticle;
        public MainPage()
        {
            InitializeComponent();
            Loaded += MainPage_Loaded;
            PageTransitionList.Completed += PageTransitionList_Completed;
            Deployment.Current.Dispatcher.BeginInvoke(LoadDataContext);
            //LoadDataContext();
        }



        private void LoadDataContext()
        {
            model = App.RssReader;
            DataContext = model;
            if (!model.IsDataLoaded)
                model.LoadData();
            if(!model.Feeds.Any())
                _buttonSettings_Click(new object(), new EventArgs());
            foreach (var feed in model.Feeds)
            {
                feed.LoadFeed();
            }

        }

        void PageTransitionList_Completed(object sender, EventArgs e)
        {
            switch (navigateToArticle)
            {
                case 1:
                    NavigationService.Navigate(new Uri("/FeedItemDetailsPage.xaml", UriKind.Relative));
                    break;
                case 2:
                    NavigationService.Navigate(new Uri("/FeedDetailsPage.xaml", UriKind.Relative));
                    break;
                case 3:
                    NavigationService.Navigate(new Uri("/ManagerRssPage.xaml", UriKind.Relative));
                    break;
                
            }
            //NavigationService.Navigate(navigateToArticle
            //                               ? new Uri("/FeedItemDetailsPage.xaml", UriKind.Relative)
            //                               : new Uri("/FeedDetailsPage.xaml", UriKind.Relative));

            var root = Application.Current.RootVisual as FrameworkElement;
            if (root != null) root.DataContext = _selectedItem;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            ResetPageTransitionList.Begin();
        }

        private void listBoxLatestNews_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var listBox = sender as ListBox;
            if (listBox == null) return;
            var selectedItem = listBox.SelectedItem as FeedItem;
            if (selectedItem == null) return;
            selectedItem.ParentFeed.CurrentFeedItem = selectedItem;
            _selectedItem = selectedItem;

            navigateToArticle = 1;
            PageTransitionList.Begin();
        }

        private void listBoxMyFeeds_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var listBox = sender as ListBox;
            if (listBox == null) return;
            _selectedItem = listBox.SelectedItem;
            if (_selectedItem == null) return;
            navigateToArticle = 2;
            PageTransitionList.Begin();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            listBoxLatestNews.SelectedIndex = -1;
            listBoxMyFeeds.SelectedIndex = -1;
            listBoxFavourite.SelectedIndex = -1;
        }

        private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var pivot = (sender as Pivot);
            if (pivot == null) return;
            ApplicationBar.IsVisible = pivot.SelectedIndex == 1;
        }

        private void _buttonSettings_Click(object sender, EventArgs e)
        {
            //NavigationService.Navigate(new Uri("/ManagerRssPage.xaml", UriKind.Relative));
            //var root = Application.Current.RootVisual as FrameworkElement;
            //if (root != null) root.DataContext = model.Feeds;
            navigateToArticle = 3;
            _selectedItem = model.Feeds;
            PageTransitionList.Begin();
        }

        private void RefreshModel(object sender, EventArgs e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
                                                          {
                                                              foreach (var feed in model.Feeds)
                                                              {
                                                                  feed.FeedItems.Clear();
                                                                  feed.GetFeedInWeb();
                                                              }
                                                          });

        }

    }
}